<?php

class Controller_Shop extends Controller_Website
{

    public function before() {
        parent::before();
        $this->section = 'shop';
        $this->template->activeCat = 'shop';
        $this->itemsPerPage = 8;
//        $this->addScript('/scripts/pgscroll_for_shop.js');
        $this->addScript('/scripts/autoScroll.js');

        //jqzoom FAILED
//        $this->addScript('/scripts/jqzoom/jquery.jqzoom-core-pack.js');
//        $this->addStyle('/styles/jqzoom/jquery.jqzoom.css');
        $this->addScript('/scripts/cloud-zoom/cloud-zoom.1.0.2.min.js');
        $this->addStyle('/styles/cloud-zoom/cloud-zoom.css');
    }

    public function actionIndex($alias='',$page = 1 , $paginator = null)
    {
        $page = (int)$page - 1;
        if(Request::$isAjax){
//            $page = (int)$page - 1;
            $this->autoRender = false;

            $category = $this->direc->getCategoryByAlias($alias, $this->section);
            $categoryId = $category['id'];

            $items = $this->manager->getCategoryItemsWithLimit($categoryId, $this->section, $order = 'id', $orderDir = 'ASC',$page,$this->itemsPerPage);
            //add images to array
            foreach($items as $key=>$value){
                $image = $this->direc->getItemImages($items[$key]['id'], $this->section, $order = 'name', $orderDir = 'ASC');
                $image = array_shift($image);
                $img_url_preview = $this->direc->imagePath($image, 'shopItemPreview', $extension = 'jpg');
                $items[$key]['img_url_preview'] = $img_url_preview;
            }

            if(isset($items)){
                foreach($items as $item){
                    echo '<div class="shop_item">
                            <div class="shop_item_image">
                                <img src="'.$item['img_url_preview'].'" />
                            </div>

                            <div class="shop_short_info">
                                <div class="shop_short_info_text">
                                    '.$item['text1'].'
                                </div>

                                <div class="shop_short_info_price">
                                    $ '.$item['price'].'
                                </div>

                                <div class="shop_add_to_cart">
                                    <span class="add_to_cart" data-id="'.$item['id'].'"></span>
                                    <span class="add_to_cart_shadow"></span>
                                </div>

                            </div>
                            <div class="quick_view">

                            </div>
                        </div>';

                }
            }
            exit();
        }

        $this->template->title = $this->settings['title'];
        $this->template->content = new View('shop/index');
        $this->addScript('/scripts/cart.js');
        //fancybox
        $this->addScript('/scripts/fancybox/jquery.fancybox.pack.js');
        $this->addStyle('/styles/fancybox/jquery.fancybox.css');
        //jscrollpane
        $this->addScript('/scripts/jscrollpane/jquery.mousewheel.js');
        $this->addScript('/scripts/jscrollpane/mwheelIntent.js');
        $this->addScript('/scripts/jscrollpane/jquery.jscrollpane.min.js');
        $this->addStyle('/styles/jquery.jscrollpane_new.css');

        $categories = $this->direc->getCategories($this->section);
        $this->template->content->categories = $categories;

        //items Count
        if(!empty ($_SESSION['cart']['shop']))
        {
            $result = 0;
            foreach ($_SESSION['cart']['shop'] as $shopItem){
                $result+=$shopItem['quantity'];
            }
            if($result > 99){
                $result = '99+';
            }
            $this->template->content->itemsCount = $result;
        }

        if($alias){

            $category = $this->direc->getCategoryByAlias($alias, $this->section);
            $categoryId = $category['id'];

//            $items = $this->direc->getCategoryItems($categoryId, $this->section, $order = 'id', $orderDir = 'ASC');
            $items = $this->manager->getCategoryItemsWithLimit($categoryId, $this->section, $order = 'id', $orderDir = 'ASC',$page,$this->itemsPerPage);
            //add images to array
            foreach($items as $key=>$value){
                $image = $this->direc->getItemImages($items[$key]['id'], $this->section, $order = 'name', $orderDir = 'ASC');
                $image = array_shift($image);
                $img_url_preview = $this->direc->imagePath($image, 'shopItemPreview', $extension = 'jpg');
                $items[$key]['img_url_preview'] = $img_url_preview;
            }

            $this->template->content->items = $items;
        }
        else{
            $first_cat = array_shift($categories);
            //temporary off
            //$this->response->redirect(Request::$controller.strtolower(substr(__FUNCTION__, 6)).'/'.$first_cat['alias'].'/');
        }

    }
}